//
//  NoteDetailviewCtrl.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/21.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "NoteDetailviewCtrl.h"
#import "NoteDetailTableview.h"
#import "NoteCommentBox.h"
#import "NoteCommentMode.h"

@interface NoteDetailviewCtrl () <LCActionSheetDelegate>

@property (weak, nonatomic) IBOutlet NoteDetailTableview *noteDetailTableview;

@property (nonatomic,strong)NoteCommentBox *commentBox;
@end

@implementation NoteDetailviewCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadTableview];
    [self layoutCommentBox];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 界面
- (void)layoutCommentBox {
    WeakSelf(ws);
    self.commentBox.isDisappear = NO;
    self.commentBox.maxVisibleLine = 3;
    self.commentBox.overflowBlock = ^{
        [SVProgressHUD showErrorWithStatus:@"input to much"];
    };
    
    self.commentBox.sendComBlock = ^(NSString *comment) {
        //发送
        [ws.view endEditing:YES];
    };
    
    [self.view addSubview:self.commentBox];
    [self.commentBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@(ws.commentBox.initHeight));
        make.width.mas_equalTo(@(kScreenWidth));
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    }];
}

- (void)reloadTableview {
    WeakSelf(ws);
    self.noteDetailTableview.noteMode = _noteMode;
    self.noteDetailTableview.beginScroll = ^(UITableView *tableView) {
        [ws.commentBox dismissFirstResponder];
    };
    
    self.noteDetailTableview.didSelectedRow = ^(UITableView *tableView, NSIndexPath *indexPath) {

        NSInteger rowIndex = indexPath.row;
        NSInteger sectionIndex = indexPath.section;
        if (sectionIndex == NoteCellSection_comment) {
            NoteCommentMode *comMode = ws.noteMode.comments[rowIndex];
            if ([[UserManager shareInstance] isSelf:comMode.user.userId]) {//删除评论
                LCActionSheet *actionSheet = [[LCActionSheet alloc] initWithTitle:@"Do you want to Delete your comment" buttonTitles:@[@"Delete"] redButtonIndex:0 delegate:ws];
                
                [actionSheet show];
                [ws.view endEditing:YES];
            } else {//评论
                if (![ws.commentBox boxisFirstResponder]) {//键盘收起状态，即将会弹起键盘
                    
                    ws.commentBox.changeFrameBlock = ^(CGFloat top) {//将tableview滚动
                        CGRect curCellRect = [tableView rectForRowAtIndexPath:indexPath];
                        CGRect revRect = [ws.noteDetailTableview convertRect:curCellRect toView:ws.view];
                        CGFloat revBottom = revRect.size.height+revRect.origin.y;
                        CGPoint conoffset = ws.noteDetailTableview.contentOffset;

                        conoffset.y += revBottom - top + 5;//5是让他向上偏移一点
                     
                        NSLog(@"conoffsetY is %f",conoffset.y);
                        NSLog(@"revBottom - top is %f",revBottom - top);
                        [ws.noteDetailTableview setContentOffset:conoffset animated:YES];
                        NSLog(@"revRect bottom is %f",revBottom);

                        NSLog(@"top is %f",top);
                    };
                    
                }
                
                [ws.commentBox reverFocus:comMode];
            }
            
        } else {
            [ws.commentBox dismissFirstResponder];
        }
    };
    [self.noteDetailTableview reloadData];
}

#pragma mark delegate :IBActionSheetDelegate
- (void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {//调用删除接口
        
        
    }
}


#pragma mark - setter
- (void)setNoteMode:(NoteDetailMode *)noteMode {
    if (_noteMode != noteMode) {
        _noteMode = noteMode;
    }
}

#pragma mark - getter 
- (NoteCommentBox *)commentBox {
    if (_commentBox == nil) {
        _commentBox =[[[NSBundle mainBundle] loadNibNamed:@"NoteCommentBox" owner:nil options:nil] lastObject];
    }
    return _commentBox;
}

#pragma mark - 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


@end
